from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from ckeditor_uploader.fields import RichTextUploadingField

class Article(models.Model):
    """
    文章模型，用于管理网站文章
    """
    title = models.CharField(_('标题'), max_length=200)
    content = RichTextUploadingField(_('内容'))
    author = models.ForeignKey(User, verbose_name=_('作者'), on_delete=models.CASCADE, related_name='articles')
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)
    is_published = models.BooleanField(_('是否发布'), default=True)
    
    class Meta:
        verbose_name = _('文章')
        verbose_name_plural = _('文章')
        ordering = ['-created_at']
    
    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        """获取文章的绝对URL"""
        return reverse('articles:detail', args=[str(self.id)])
